package cn.chenwx.bbsforum.controller;

import cn.chenwx.bbsforum.entity.Article;
import cn.chenwx.bbsforum.entity.User;
import cn.chenwx.bbsforum.mapper.TagMapper;
import cn.chenwx.bbsforum.service.ArticleService;
import cn.chenwx.bbsforum.service.TagService;
import cn.chenwx.bbsforum.service.UserService;
import cn.chenwx.bbsforum.utils.APIResponse;
import cn.chenwx.bbsforum.utils.BlogJSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class ArticleController {

    @Autowired
    ArticleService articleService;
    @GetMapping("/bloglist")
    public String publishArticle(){
        return "admin/blogList";
    }

    @RequestMapping("/getBlogList")
    @ResponseBody
    public Map<String,Object> getBlogList(int page,int limit){
        PageInfo<Article> articlePageInfo = new PageInfo<>(articleService.findAllArticle(limit,page));
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("code",0);
        map.put("data",articlePageInfo.getList());
        map.put("count",articlePageInfo.getTotal());
        return map;
    }

    @RequestMapping("/deleteArticle")
    @ResponseBody
    public BlogJSONResult deleteArticle(@RequestParam("id") String articleId){
        return articleService.deleteArticle(Integer.parseInt(articleId));
    }
    @Autowired
    private TagMapper tagMapper;
    @Autowired
    private TagService tagService;
    @Autowired
    private UserService userService;

    @RequestMapping("/getTag")
    @ResponseBody
    public BlogJSONResult getTag(){
        return BlogJSONResult.ok(tagMapper.findTags()) ;
    }
    @PostMapping("/searchArticle")
    @ResponseBody
    public Map<String,Object> searchArticle(String categoryName,Integer page,Integer limit) {
        PageInfo<Article> articlePageInfo = new PageInfo<>(articleService.findArticleByTag(categoryName,limit,page));
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("code",0);
        map.put("data",articlePageInfo.getList());
        map.put("count",articlePageInfo.getTotal());
        return map;
    }

    @GetMapping("/editor")
    public String editor(Model model, HttpSession session){
        String name= (String) session.getAttribute("username");
        System.out.println(name);
//        User user = userService.findUserByName(name);
//        System.out.println(user.getId());
//       Article article= articleService.findArticleByUserId(user.getId());
//        model.addAttribute("article",article);

        return "admin/test";
    }


    @GetMapping(value = "/findTagName")
    @ResponseBody
    public BlogJSONResult findTagName(){
        return BlogJSONResult.ok(tagMapper.findTagName());
    }






}
